package cn.clq.ymcc.web.controller;

import cn.clq.ymcc.dto.KillParamDto;
import cn.clq.ymcc.service.IKillCourseService;
import cn.clq.ymcc.domain.KillCourse;
import cn.clq.ymcc.query.KillCourseQuery;
import cn.clq.ymcc.result.JSONResult;
import cn.clq.ymcc.result.PageList;
import cn.clq.ymcc.vo.CourseOrderVo;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    /**
     * @description: 查询订单结算页所需数据
     * @param: orderNo
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/8/7 1:03
     */
    @GetMapping("/online/oneByOrder/{orderNo}")
    public JSONResult oneByOrder(@PathVariable("orderNo") String orderNo){
        CourseOrderVo vo = killCourseService.oneByOrder(orderNo);
        return JSONResult.success(vo);
    }


    /**
     * @description: 秒杀下单，返回预创订单的订单号
     * @param: dto
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/8/7 0:19
     */
    @PostMapping("/kill")
    public JSONResult kill(@RequestBody KillParamDto dto){
        String orderNo = killCourseService.kill(dto);
        return JSONResult.success(orderNo);
    }
    /**
     * @description: 查询秒杀课程的详情页数据
     * @param: activityId   killId
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/8/6 23:52
     */
    @GetMapping("/online/one/{activityId}/{killId}")
    public JSONResult onlineOne(@PathVariable("activityId")Long activityId,
                                @PathVariable("killId")Long killId){
        KillCourse killCourse = killCourseService.onlineOne(activityId,killId);
        return JSONResult.success(killCourse);
    }

    /**
     * @description: 查询所有秒杀课程
     * @param:
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/8/6 19:31
     */
    @GetMapping("/online/all")
    public JSONResult onlineAll(){
        List<KillCourse> killCourseList= killCourseService.onlineAll();
        return JSONResult.success(killCourseList);
    }

    /**
     * @description: 给秒杀活动新增课程
     * @param: killCourse
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/8/6 1:03
     */
    @PostMapping("/save")
    public JSONResult save(@RequestBody KillCourse killCourse){
            killCourseService.save(killCourse);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JSONResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
